package cn.whdreamblog.daoimp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.text.StyledEditorKit.BoldAction;

import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;

import cn.whdreamblog.dao.BlogArticleDAO;
import cn.whdreamblog.entites.BlogArticle;
import cn.whdreamblog.entites.PageBean;
import cn.whdreamblog.util.BlogUtil;
import cn.whdreamblog.util.MyDBUtil;

public class BlogArticleDAOImp extends BaseModelDAOImp implements BlogArticleDAO {
	
	public BlogArticleDAOImp() {
		super();
	}
	
	@Override
	public boolean insertBlogArticle(BlogArticle ba) {
		return super.insertData(ba);
	}

	@Override
	public boolean deleteBlogArticle(BlogArticle ba) {
		return super.removeData(ba);
	}

	@Override
	public boolean updateBlogArticle(BlogArticle ba) {
		return super.updateData(ba);
	}
	
	@Override
	public List<BlogArticle> searchBlogArticleByTitle(String title) {
		return this.searchBlogArticleByAttr(title);
	}

	@Override
	public List<BlogArticle> searchBlogArticleByKind(String kind) {
	  return this.searchBlogArticleByAttr(kind);
	}

	@Override
	public List<BlogArticle> searchBlogArticleByPubDate(String pubDate) {
		return this.searchBlogArticleByAttr(pubDate);
	}

	@Override
	public BlogArticle searchBlogArticleById(int id) {
		return super.getDataById(id,BlogArticle.class);
	}
	/**
	 * 已经被getAllBlogSomeArticles取代
	 */
	@Override @Deprecated
	public List<BlogArticle> getAllBlogArticles() {
		if(sessionFactory!=null){
			session=sessionFactory.openSession();
			try {
				tx=session.beginTransaction();
				List<BlogArticle> list = session.createQuery("from BlogArticle order by id desc")
										.list();
				tx.commit();
				if(list!=null&&list.size()>0){
					
					return list;
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return null;
		
	}
	@Override
	public List<BlogArticle> getAllBlogSomeArticles( int start, int limit) {
		return super.getSomeData(start, limit,BlogArticle.CLASS_NAME);
	}
	
	@Override
	public List<BlogArticle> getSomeBlogArticlesAttribute(int start, int limit,
			String attr) {
		if(sessionFactory!=null){
			session=sessionFactory.openSession();
			
			try {
				tx=session.beginTransaction();
				List<BlogArticle> list = this.getBlogsByAttribute(session, start, limit, attr);			
				tx.commit();
				if(list!=null&&list.size()>0){
					return list;
				}else{
					return null;
				}
				
			} catch (Exception e) {
				LOG.error("getSomeBlogArticlesAttribute:",e);
				e.printStackTrace();
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return null;
	}
	
	private List<BlogArticle> getBlogsByAttribute(Session session,int start, int limit,
			String attr) {
		Query query =null;
		Long count = (Long)session
                .createQuery("select count(*) from BlogArticle")//统计条数
                .uniqueResult();
		if(attr!=null&&attr.length()>0){
			query = session.createQuery("select id,"+attr+" from BlogArticle  order by id desc");
		}else {
			query = session.createQuery(" from BlogArticle  order by id desc");
		}
		
		if(count>limit){
			query.setFirstResult(start);
			query.setMaxResults(limit);
		}
		List<BlogArticle> list =query.list();	
		return list;
	}
	
	private List<BlogArticle> searchBlogArticleByAttr(String attr) {
		if(sessionFactory!=null){
			session=sessionFactory.openSession();
			try {
				tx=session.beginTransaction();
				List<BlogArticle> list = session.createQuery("from BlogArticle b where pubdate like '%"+attr
						+"%' or kind like '%"+attr+"%' or title like '%"+attr+"%'")
							.list();
				tx.commit();
				if(list!=null&&list.size()>0){
					return list;
				}
				
			} catch (Exception e) {
				LOG.error("searchBlogArticleByAttr:",e);
				e.printStackTrace();
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return null;
	}
	
	private Map searchBlogArticleByAttr2Map(String attr, int limit,int start) {
		if(sessionFactory!=null){
			session=sessionFactory.openSession();
			Query query = null;
			try {
				tx=session.beginTransaction();
				Long count = (Long) session.createQuery(
						"select count(*)" + "from BlogArticle b where pubdate like '%"+attr
						+"%' or kind like '%"+attr+"%' or title like '%"+attr+"%'")// 统计条数
						.uniqueResult();
				query = session.createQuery("from BlogArticle b where pubdate like '%"+attr
						+"%' or kind like '%"+attr+"%' or title like '%"+attr+"%'");
				if (count > limit) {
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List<BlogArticle> list = query.list();
				tx.commit();
				if(list!=null&&list.size()>0){
					Map<String,Object> map = new HashMap<String, Object>();
					map.put("count", count);
					map.put("list", list);
					return map;
				}
				
			} catch (Exception e) {
				LOG.error("searchBlogArticleByAttr2Map:",e);
				e.printStackTrace();
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return null;
	}
	
	/**
	 * 使用pageBean重构分页代码
	 */
	@Override
	public PageBean getAllBlogSomeArticlesByPage(
			int limit,int page) {
		PageBean pageBean = new PageBean();
		Map map = super.getSomeData2Map(PageBean.countOffset(limit, page), limit, BlogArticle.CLASS_NAME);
		pageBean = PageBean.setPageData((List)map.get("list"),pageBean,(Long)map.get("count"), limit, page);
		return pageBean;
	}
	
	@Override
	public PageBean searchBlogArticleByAttrAndPage(String attr, int limit ,int page){
		Map map = this.searchBlogArticleByAttr2Map(attr, limit, PageBean.countOffset(limit, PageBean.countCurrentPage(page)));
		PageBean pageBean = new PageBean();
		if(map == null){
			return null;
		}
		pageBean = pageBean.setPageData(
				(List)map.get("list"),pageBean,(Long)map.get("count"), limit, page);
		return pageBean;
	}
	
	
	
}
